package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IUnicomRegionService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegionQry;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalCityAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName : LocalEparchyAttrLinkHandler
 * @Description : 装机地址地市属性联动处理类
 * @Author : xie.zhengjun
 * @Date : 2020/9/22 17:29
 */
@Service
@AttrHandler(LocalCityAttrHandler.class)
public class LocalCityAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Resource
    private IUnicomRegionService unicomRegionService;
    @Resource
    private OperateInstAttrService operateInstAttrService;
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String areaFieldName = null;
        if ("2_0000109".equals(attr.getFieldName())) {
            //区县（Z端）
            areaFieldName = "2_0000111";
        }
        else if ("2_0000221".equals(attr.getFieldName())) {
            //区县
            areaFieldName = "2_0000222";
        }
        if (StrTools.isEmpty(areaFieldName)) {
            return;
        }

        KiteAttr area = inst.getAttr(areaFieldName);
        if (Objects.isNull(area)) {
            return;
        }
        // 地市改动则清空区县、装机地址
        operateInstAttrService.setAttrValue(inst, areaFieldName, "", data.getSceneInstId(), null);
//        operateInstAttrService.setAttrValue(inst, "2_0000211", "", data.getSceneInstId(), null);
        // 选定地市后联动出可选区县
        if (KiteStringUtils.isNotEmpty(attr.getValue())) {
            List<UnicomRegion> unicomRegions = unicomRegionService.subRegionFromPlatform(new UnicomRegionQry(attr.getValue(), "30", inst.getSpecId()));
            if (KiteListUtils.isEmpty(unicomRegions)) {
                return;
            }
            List<KiteAttrValue> kiteAttrValues = unicomRegions.stream().map(unicomRegion -> {
                KiteAttrValue kiteAttrValue = new KiteAttrValue();
                kiteAttrValue.setAttrValue(unicomRegion.getRegionCode());
                kiteAttrValue.setAttrValueName(unicomRegion.getRegionName());
                return kiteAttrValue;
            }).collect(Collectors.toList());

            area.setAttrValues(kiteAttrValues);
        }
        else {
            area.setAttrValues(new ArrayList<>());
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
